package com.eslserver.servletapi;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.esl.coordinator.command.Command;
import com.esl.coordinator.command.CommandInvoker;
import com.esl.coordinator.command.CommandReceiver;
import com.esl.coordinator.command.STiTEQATMELCommandFactory;
import com.esl.coordinator.command.SocketCommandReceiver;
import com.esl.coordinator.exception.STiTEQATMELIntegrationException;
import com.eslserver.servletapi.input.ChangeRFChannelInputParam;
import com.eslserver.servletapi.response.ChangeRFChannelReturnValue;

@SuppressWarnings("serial")
public class ChangeRFChannelServlet extends HttpServlet implements
		ChangeRFChannelInputParam, ChangeRFChannelReturnValue {
	public static final Logger LOG = LoggerFactory
			.getLogger(ChangeRFChannelServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String returnMessage = new String(OK);
		PrintWriter pw = resp.getWriter();

		ChangeRFChannelInputWrapper changeRFChannelInputWrapper = new ChangeRFChannelInputWrapper();

		if (req.getParameter(COORDINATOR_IP_ADDRESS) != null) {
			changeRFChannelInputWrapper.setCoordinatorIp(req
					.getParameter(COORDINATOR_IP_ADDRESS));
		}
		if (req.getParameter(COORDINATOR_PORT) != null) {
			changeRFChannelInputWrapper.setCoordinatorPort(Integer.valueOf(req
					.getParameter(COORDINATOR_PORT)));
		}
		if (req.getParameter(CHANNEL_NUMBER) != null) {
			changeRFChannelInputWrapper.setChannelNumber(Integer.valueOf(req
					.getParameter(CHANNEL_NUMBER)));
		}

		CommandReceiver commandReceiver = SocketCommandReceiver.getInstance(
				changeRFChannelInputWrapper.getCoordinatorIp(),
				changeRFChannelInputWrapper.getCoordinatorPort());
		Command changeRFChannelCommand = STiTEQATMELCommandFactory
				.getInstance().createRFChannelCommand(
						changeRFChannelInputWrapper.getChannelNumber(),
						commandReceiver);
		try {
			CommandInvoker.sendCommand(changeRFChannelCommand);

			if (!changeRFChannelCommand.isCommandSuccessful()) {
				returnMessage = ERROR;
			}
		} catch (STiTEQATMELIntegrationException e) {
			LOG.error(e.getMessage(), e);
			returnMessage = ERROR;
		} catch (IOException e) {
			LOG.error(e.getMessage(), e);
			returnMessage = CONNECTION_FAIL;
		}

		pw.write(returnMessage);
		pw.close();
	}

	private class ChangeRFChannelInputWrapper {
		private Integer channelNumber;
		private String coordinatorIp;
		private Integer coordinatorPort;

		public Integer getChannelNumber() {
			return channelNumber;
		}

		public void setChannelNumber(Integer channelNumber) {
			this.channelNumber = channelNumber;
		}

		public String getCoordinatorIp() {
			return coordinatorIp;
		}

		public void setCoordinatorIp(String coordinatorIp) {
			this.coordinatorIp = coordinatorIp;
		}

		public Integer getCoordinatorPort() {
			return coordinatorPort;
		}

		public void setCoordinatorPort(Integer coordinatorPort) {
			this.coordinatorPort = coordinatorPort;
		}
	}

}
